﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Net;
using Newtonsoft.Json.Linq;
using System.Text;

public partial class news : Basic
{
    public string newsList = "";
    public string cpbdList = "";
    public string jdtjList = "";
    public string ydphList = "";
    public string ltjhList = "";
    public string _m = "00";  //频道
    public string strChannel = "";
    public string _tid = "0101";  //类别
    public string strClass = "";
    public string strCName = "";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            _tid = OTO.ToStr(Request["cid"], "0101");
            _m = OTO.ToStr(Request["m"], _m);

            Dictionary<string, ChannelEnty> dict = DictChannel();
            strChannel = "<a href=\"" + dict[_m].Url + "\" class=\"w\">" + dict[_m].Name + "</a>";

            string cname = Pub.LoadData(jsonUrl + "news/TypeNameByTypeId.ashx?typeid=" + _tid);
            strClass = "<a href=\"news_" + _m + "_" + _tid + "_1.shtml\" class=\"w\">" + cname + "</a>";
            strCName = cname;

            _title = dict[_m].Name + "-" + cname + "-益盟操盘手";
            _keywords = dict[_m].Name + "，股票,行情,软件,金融,证券,股票论坛,股票软件,证券分析软件,手机炒股,益盟操盘手,操盘手软件,益盟官网";
            _desc = "益盟操盘手:您身边的股票决策师,中国专业股票门户网站,提供权威财经资讯、高速实时行情、免费炒股软件，并为用户提供投资和理财交流论坛";

        }
        catch { }

        newsList = sbNewsList().ToString();

        cpbdList = sbCPBDList().ToString();

        jdtjList = sbJDTJList().ToString();

        ydphList = sbYDPHList().ToString();

        ltjhList = sbLTJHList().ToString();

        BasicHeader(_title, string.Empty, string.Empty,_desc,_keywords);
    }

    private StringBuilder sbNewsList()
    {
        StringBuilder sb = new StringBuilder();
        try
        {
            string url = jsonUrl + "news/NewsList.ashx?typeid=" + _tid;
            string str = Pub.LoadData(url);
            NewsList obj = JsonConvert.DeserializeObject<NewsList>(str);
            if (obj.list.Length > 0)
            {

                int _totalCount = 0;
                int _pagesize = 50;
                int _totalPage = 0;
                _totalCount = obj.list.Length;
                if (_totalCount > 0)
                {
                    if ((_totalCount % _pagesize) == 0)
                        _totalPage = _totalCount / _pagesize;
                    else
                        _totalPage = (int)(_totalCount / _pagesize) + 1;
                }
                int page = OTO.ToInt(Request["page"], 1);
                if (page == 0)
                    page = 1;
                if (page > _totalPage)
                    page = _totalPage;
                int _index = (page - 1) * _pagesize;
                int _end = page * _pagesize;
                if (page == _totalPage && (_totalCount % _pagesize) != 0)
                    _end = (page - 1) * _pagesize + _totalCount % _pagesize;

                string strPage = "<div class=\"news_next\">" + PageHelper.fy_NewsList(_totalCount, _pagesize, page, Request.Url.AbsoluteUri, _tid, _m) + "</div>";
                sb.AppendLine(strPage);
                sb.AppendLine("<div class=\"clearok\"></div>");

                sb.Append("<ul class=\"news_magin10\">");
                for (int i = _index; i < _end; i++)
                {
                    string fc = "class=\"b\"";
                    if (i < 5)
                        fc = "class=\"r\"";
                    sb.AppendLine("<li> ·<a href=\"" + GetNewsUrl(_m, _tid, obj.list[i].Id) + "\" " + fc + ">" + obj.list[i].Title + "</a><span class=\"news_l_rq\">" + OTO.ToDate(obj.list[i].PTime, DateTime.Now).ToString("yyyy-MM-dd") + "</span></li>");
                    if ((i + 1) % 5 == 0)
                        sb.AppendLine("<li class=\"line\"></li>");
                }
                sb.Append("</ul>");
                sb.AppendLine("<div class=\"clearok\"></div>");
                sb.AppendLine(strPage);
            }
        }
        catch { }
        return sb;
    }

    private StringBuilder sbCPBDList()
    {
        StringBuilder sb = new StringBuilder();
        try
        {
            string url = jsonUrl + "comm/module.ashx?id=cpbb";
            string str = Pub.LoadData(url);
            List<List<NewsSBEnty>> obj = JsonConvert.DeserializeObject<List<List<NewsSBEnty>>>(str);
            foreach (var item in obj)
            {
                sb.AppendFormat("<li>");
                for (int i = 0; i < item.Count; i++)
                {
                    sb.AppendFormat("<a href=\"{1}\"><span style=\"color:{2};font-weight:{3}\">{0}</span></a> ", item[i].Text, item[i].Url, item[i].Color, item[i].FontWeight);
                }
                sb.AppendFormat("</li>");
                //sb.AppendFormat("<br />");
            }
        }
        catch { }
        return sb;

    }

    private StringBuilder sbJDTJList()
    {
        StringBuilder sbCPBD = new StringBuilder();
        try
        {
            string url = jsonUrl + "comm/module.ashx?id=jdtj";
            string str = Pub.LoadData(url);
            List<List<NewsSBEnty>> obj = JsonConvert.DeserializeObject<List<List<NewsSBEnty>>>(str);
            int i = 0;
            foreach (var item in obj)
            {
                //if (item.Count == 3)
                //{
                sbCPBD.Append("<li><a href=\"" + item[0].Url + "\" class=\"b\">" + item[0].Text + "</a>&nbsp;</li>");
                //sbCPBD.Append("<a href=\"" + item[1].Url + "\" class=\"b\">" + item[1].Text + "</a>&nbsp;");
                //sbCPBD.Append("<a href=\"" + item[2].Url + "\" class=\"b\">" + item[2].Text + "</a>&nbsp;");
                i++;
                if (i >= 6)
                    break;
                //}
            }
        }
        catch { }
        return sbCPBD;
    }

    private StringBuilder sbYDPHList()
    {
        StringBuilder sb = new StringBuilder();
        try
        {
            string url = jsonUrl + "news/ydph.ashx";
            string str = Pub.LoadData(url);
            List<NewsListEnty> list = JsonConvert.DeserializeObject<List<NewsListEnty>>(str);
            int len = 7;
            if (list.Count < len)
                len = list.Count;
            sb.Append("<ul class=\"font12\">");
            for (int i = 0; i < len; i++)
            {
                sb.AppendFormat("<li> ·<a href=\"{1}\">{0}</a> </li>", list[i].Title, GetNewsUrl(_m, _tid, list[i].Id));
            }
            sb.Append("</ul>");
        }
        catch { }
        return sb;
    }

    private StringBuilder sbLTJHList()
    {
        StringBuilder sb = new StringBuilder();
        try
        {
            string url = jsonUrl + "news/ltjh.ashx";
            string str = Pub.LoadData(url);
            List<NewsListEnty> list = JsonConvert.DeserializeObject<List<NewsListEnty>>(str);
            int len = 7;
            if (list.Count < len)
                len = list.Count;
            sb.Append("<ul class=\"font12\">");
            for (int i = 0; i < len; i++)
            {
                sb.AppendFormat("<li> ·<a href=\"{1}\">{0}</a> </li>", list[i].Title, list[i].Url);
            }
            sb.Append("</ul>");
        }
        catch
        { }
        return sb;
    }

}